package video

import (
	"github.com/labstack/echo"
	resp "tacitus/library/response"
	"time"
)

// 显示分类的面包屑导航
// 因为使用了链接的原因，只输入分类id无法确定上级路径，所以需要传入全部路径
func ShowBreadcrumbsHandler(c echo.Context) error {
	i := new(struct{
		CateIDList []int64 `json:"cate_id_list"`
	})
	if err := c.Bind(i); err != nil {
		return err
	}
	data := make([]Category, 0)
	if err := rx.In("id", i.CateIDList).Find(&data); err != nil {
		return err
	}
	// 将结果按照输入顺序排序
	m := make(map[int64]Category)
	for _, item := range data {
		m[item.ID] = item
	}
	res := make([]Category, len(i.CateIDList))
	for index, id := range i.CateIDList {
		res[index] = m[id]
	}
	return resp.Data(c, res)
}

func CreateLinkHandler(c echo.Context) error {
	i := new(Link)
	if err := c.Bind(i); err != nil {
		return err
	}
	i.ID = 0
	i.CreateTime = time.Now().Unix()
	if _, err := rx.Insert(i); err != nil {
		return err
	}
	return nil
}